package com.san.oms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.base.service.CodeService;
import com.san.common.utils.StringUtils;
import com.san.oms.domain.ScoreDTO;
import com.san.oms.service.ScoreService;

/**
 * 信用分业务控制类
 * 
 * @author xiangyz
 *
 */
@RequestMapping("/ce/score")
@Controller
public class ScoreController extends BaseController{

	private static final Logger logger = LoggerFactory.getLogger(ScoreController.class);

	private String PREFIX = "ce/score";

	@Autowired
	ScoreService scoreService;
	
	@Autowired
	CodeService codeService;

	@GetMapping("")
	@RequiresPermissions("ce:score:view")
	String source(Model model) {
		return PREFIX + "/view";
	}
	
	/**
	 * 获取指标模型数据详情
	 * 
	 * @return
	 */
	@PostMapping("scoreViewDefault")
	@RequiresPermissions("ce:quota:view")
	@ResponseBody
	ScoreDTO scoreViewDefault(@RequestParam Map<String, Object> params) {
		ScoreDTO scoreDTO = null;
		try {
			String modelId = codeService.getByName("report_legalperson_model_id").getCodeValue();
			String name = (String) params.get("name");
			String code = "";
			if (null == modelId || (StringUtils.isEmpty(name) && StringUtils.isEmpty(code))) {
				return new ScoreDTO();
			}
			
			scoreDTO = scoreService.queryScore(Long.parseLong(modelId), name, code);
		} catch (Exception e) {
			scoreDTO = new ScoreDTO();
			logger.error("获取模型分数出错", e);
		}
		return scoreDTO;
	}
	
	/**
	 * 获取指标模型数据详情
	 * 
	 * @return
	 */
	@PostMapping("scoreView")
	@RequiresPermissions("ce:quota:view")
	@ResponseBody
	ScoreDTO scoreView(@RequestParam Map<String, Object> params) {
		ScoreDTO scoreDTO = null;
		try {
			String modelId = (String) params.get("modelId");
			String name = (String) params.get("name");
			String code = (String) params.get("code");
			if (null == modelId || (StringUtils.isEmpty(name) && StringUtils.isEmpty(code))) {
				return new ScoreDTO();
			}
			
			scoreDTO = scoreService.queryScore(Long.parseLong(modelId), name, code);
		} catch (Exception e) {
			scoreDTO = new ScoreDTO();
			logger.error("获取模型分数出错", e);
		}
		return scoreDTO;
	}

	/**
	 * 获取指标模型数据详情
	 * 
	 * @return
	 */
	@GetMapping("scoreList")
	@RequiresPermissions("ce:quota:view")
	@ResponseBody
	List<ScoreDTO> scoreList(@RequestParam Map<String, Object> params) {
		List<ScoreDTO> list = null;
		try {
			String modelId = (String) params.get("modelId");
			String name = (String) params.get("name");
			String code = (String) params.get("code");
			if (null == modelId || (StringUtils.isEmpty(name) && StringUtils.isEmpty(code))) {
				return new ArrayList<ScoreDTO>();
			}
			
			list = scoreService.queryScoreDetail(Long.parseLong(modelId), name, code);
		} catch (Exception e) {
			list = new ArrayList<ScoreDTO>();
			logger.error("获取模型分数出错", e);
		}

		return list;
	}

}
